package com.ram.testcoherence.ejb;

import com.ram.testcoherence.PersonCacheListener;
import com.ram.testcoherence.entity.Person;

import com.tangosol.net.CacheFactory;

import com.tangosol.net.NamedCache;

import com.tangosol.util.MapEvent;

import java.util.List;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless(name = "CoherenceSessionEJB", mappedName = "CoherenceSessionEJB")
public class CoherenceSessionEJBBean implements CoherenceSessionEJB {
    @PersistenceContext(unitName="TestCoherenceProject")
    private EntityManager em;
    NamedCache nc = CacheFactory.getCache("Person");
    public CoherenceSessionEJBBean() {
        nc.addMapListener(new PersonCacheListener()); 
    }

    public Person persistPerson(Person person) {
        em.persist(person);
        return person;
    }

    public Person mergePerson(Person person) {
        return em.merge(person);
    }

    public void removePerson(Person person) {
        person = em.find(Person.class, person.getPersonid());
        em.remove(person);
    }

    /** <code>select o from Person o</code> */
    public List<Person> getPersonFindAll() {
        return em.createNamedQuery("Person.findAll").getResultList();
    }
    
    public Person getPersonFromCache(Long personId){
        Person person = (Person) nc.get(personId);
        return person;
    }
}
